package com.uenpay.oem.ui.auth;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uenpay.im.utilities.Toasts;
import com.uenpay.oem.R;
import com.uenpay.oem.data.bean.UserIdCardInfo;
import com.uenpay.util.app.base.BaseFragment;
import com.uenpay.util.common.EditFormatUtils;
import com.uenpay.util.common.VerifyUtils;
import com.uenpay.util.widget.UenEditTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Author: liulou
 * Date: 2017/6/16.
 * desc: 确认实名认证身份信息界面
 */

public class AuthConfirmFragment extends BaseFragment implements AuthComfirmContract.View {

    private static final String TAG = "AuthConfirmFragment";

    @BindView(R.id.iv_progress_header)
    ImageView ivProgressHeader;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_idcard_num)
    EditText etIdcardNum;
    @BindView(R.id.uen_et_layout)
    UenEditTextView uenEtLayout;
    @BindView(R.id.ll_idcard_info)
    LinearLayout llIdcardInfo;
    @BindView(R.id.btn_commit_auth)
    Button btnCommitAuth;

    private Unbinder mUnbinder;
    private AuthComfirmContract.Presenter mConfirmPresenter;
    private static AuthFragmentChangeCallback mCallBack;

    private AlertDialog errorDialog;

    private UserIdCardInfo mIdCardInfo;

    public static AuthConfirmFragment newInstance(AuthFragmentChangeCallback callback) {
        mCallBack = callback;
        return new AuthConfirmFragment();
    }


    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.fragment_auth_confirm);
        mUnbinder = ButterKnife.bind(this, getContentView());
        initView();
    }

    private void initView() {
        setPresenter(null);
        EditFormatUtils.idCardAddSpace(etIdcardNum);
        if (getArguments().containsKey("info")) {
            mIdCardInfo = getArguments().getParcelable("info");
            etIdcardNum.setText(mIdCardInfo.getCerNo());
            etName.setText(mIdCardInfo.getCerName());
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void setPresenter(Object presenter) {
        mConfirmPresenter = new AuthConfirmPresenter(this);
    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void showAuthResult(boolean res) {
        if (res) {
            if (mCallBack != null) {
                mCallBack.authSuccess();
            }
        } else {
            showFailedDialog();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mCallBack != null) {
            mCallBack = null;
        }
        mUnbinder.unbind();
    }

    private void showFailedDialog() {
        if (errorDialog == null) {
            View errView = LayoutInflater.from(getActivity()).inflate(R.layout.include_auth_result_fail, null);
            errorDialog = new AlertDialog.Builder(getActivity()).setCancelable(false).setView(errView).create();
            errView.findViewById(R.id.tv_retry).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    errorDialog.dismiss();
                }
            });
        }
        errorDialog.show();
    }

    @OnClick({R.id.et_name, R.id.et_idcard_num, R.id.btn_commit_auth, R.id.ibtn_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et_name:
                etName.setCursorVisible(true);
                break;
            case R.id.et_idcard_num:
                etIdcardNum.setCursorVisible(true);
                break;
            case R.id.btn_commit_auth:
                commitAuth();
                break;
            case R.id.ibtn_clear:
                etIdcardNum.setCursorVisible(true);
                etIdcardNum.setText("");
                break;
        }
    }

    private void commitAuth() {
        String name = etName.getText().toString().trim();
        String num = etIdcardNum.getText().toString().trim().replace(" ", "");
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(num)) {
            Toasts.shortToast(getActivity(), "信息不能为空");
            return;
        }
        if (VerifyUtils.verifyIDCard(num) != 0) {
            Toasts.shortToast(getActivity(), "请确认身份证号码是否正确");
            return;
        }
        mConfirmPresenter.confirmAuth(new UserIdCardInfo(num, name));
    }
}
